home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_06 / saks / showheap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-08  |  1.3 KB  |  75 lines

  1. Listing 7 - a heap monitoring function
  2.  
  3. //
  4. // showheap.cpp - display the contents of the heap
  5. //
  6.  
  7. #if defined(__TURBOC__)
  8. #include <alloc.h>
  9. #include <stdio.h>
  10.  
  11. void showheap()
  12.     {
  13.     static int first = 1;
  14.     static void *p = 0;
  15.     char s[40];
  16.     struct heapinfo hi;
  17.     if (heapcheck() == _HEAPOK)
  18.         {
  19.         hi.ptr = p;
  20.         cout << "   addr   size   status\n";
  21.         while (heapwalk(&hi) == _HEAPOK)
  22.             {
  23.             if (first)
  24.                 p = hi.ptr;
  25.             sprintf(s, "%7p%7lu   %s\n",
  26.                 hi.ptr, (unsigned long)hi.size,
  27.                 hi.in_use ? "used" : "free");
  28.             cout << s;
  29.             }
  30.         first = 0;
  31.         }
  32.     else if (heapcheck() == _HEAPCORRUPT)
  33.         cout << "heap is corrupt!\n";
  34.     }
  35.  
  36. #elif defined(_MSC_VER) || defined(__WATCOMC__)
  37. #include <malloc.h>
  38. #include <stdio.h>
  39.  
  40. void showheap()
  41.     {
  42.     static int first = 1;
  43. #if defined(_MSC_VER)
  44.     static int far *p = 0;
  45. #elif defined(__WATCOMC__)
  46.     static void far *p = 0;
  47. #endif
  48.     char s[40];
  49.     _HEAPINFO hi;
  50.     if (_heapchk() == _HEAPOK)
  51.         {
  52.         hi._pentry = p;
  53.         cout << "   addr   size   status\n";
  54.         while (_heapwalk(&hi) == _HEAPOK)
  55.             {
  56.             if (first)
  57.                 p = hi._pentry;
  58.             sprintf(s, "%7Fp%7lu   %s\n",
  59.                 hi._pentry, (unsigned long)hi._size,
  60.                 hi._useflag ? "used" : "free");
  61.             cout << s;
  62.             }
  63.         first = 0;
  64.         }
  65.     else if (_heapchk() != _HEAPEND)
  66.         cout << "heap is corrupt!\n";
  67.     }
  68.  
  69. #else
  70.  
  71. void showheap() { }
  72.  
  73. #endif
  74.  
  75.